var mongodb = require('./mongodb');

var Schema = mongodb.mongoose.Schema;  // 模式、策略

var DeviceInfoSchema = new Schema({
	appName: String,
	userName: String,
	deivceName: String,
	deviceId: String,
	osVersion: String, 
	pushToken: String, // 鉴权产生的Token
	tags: [],
	connectTime: { type: Date, default: Date.now}, // 最近签到时间，如果重新连接，更新时间和online
	disconnetTime: { type: Date, default: Date.now}, // 离线时间：统计在线时长：1.在线情况，now-connectTime 2.离线情况：disconnetTime-connetTime
	online: { type: Boolean, default: true} // 断开连接不删除，即更新为false
});

var DeviceInfo = mongodb.mongoose.model("DeviceInfo", DeviceInfoSchema); // 模型

var DeviceInfoDAO = function(){};

/**
 * Middleware oprate...
 *
	DeviceInfoSchema.pre('save', function(next){
		// do stuff
		next();
	});

	DeviceInfoSchema.statics = {
		fetch: function(cb) {
			return this.find.sort('xxx')
				exec(cb);
		},
		findById: function(id, cb) {
			return this.findOne({_id: id})
				exec(cb);
		}
	};
 */

/**
 * [save description] 插入一条记录
 * @param  {[type]}   obj      [description] obj 即json对象
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
DeviceInfoDAO.prototype.save = function(obj, callback) {
	var instance = new DeviceInfo(obj);

	instance.save(function(err) {
		callback(err);
	});
};

/**
 * [delete description] 删除一条记录
 * @param  {[type]} conditions [description] conditions 删除的条件 eg:{_id: id}、{ title: 'abc' }
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
DeviceInfoDAO.prototype.delete = function(conditions, callback) {
	DeviceInfo.remove(conditions, function(err, DeviceInfo) {
		callback(err, DeviceInfo);
	});
};

/**
 * [save description] 根据id查找对象
 * @param  {[type]}   id      [description] id 即json对象id
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
DeviceInfoDAO.prototype.findById = function(id, callback) {
	DeviceInfo.findById(id, function(err, DeviceInfo) {
		callback(err, DeviceInfo);
	});
};

/**
 * [findByName description] 根据某个字段的名称查询
 * @param  {[type]}   cloumName [description]
 * @param  {[type]}   name      [description]
 * @param  {Function} callback  [description]
 * @return {[type]}             [description]
 */
DeviceInfoDAO.prototype.findByName = function(cloum_Name, callback) {
	// console.log(arguments);
	DeviceInfo.findOne(cloum_Name, function(err, obj) {
		// console.log("length = " + arguments.callee.length);
		callback(err, obj);
	});
};

/**
 * [findOneAndUpdate description] 根据属性查找对象并且更新
 * @param  {[type]}   conditions [description] 匹配属性 { name: 'borne' }
 * @param  {[type]}   update     [description] 更新属性 { name: 'jason borne' }
 * @param  {[type]}   options    [description]  
 * @param  {Function} callback   [description]
 * @return {[type]}              [description]
 */
DeviceInfoDAO.prototype.findOneAndUpdate = function(conditions, update, callback) {
	DeviceInfo.findOneAndUpdate(conditions, update, function(err, DeviceInfo) {
		// console.log(arguments);
		// console.log("length = " + arguments.callee.length);
		callback(err, DeviceInfo);
	});
};

module.exports = new DeviceInfoDAO();

